package com.maxuxu.scihome.controller.user;

import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.pojo.User;
import com.maxuxu.scihome.service.MailService;
import com.maxuxu.scihome.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: UserController
 * @author: Maxuxu
 * @date: 2022/6/29 15:55
 * @description:
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    Message message;
    @Autowired
    User user;

    @Autowired
    MailService mailService;

    /**
     * 修改密码业务
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/user/editPassword")
    public Message editPassword(HttpServletRequest request, HttpSession session){
        String newPassword = request.getParameter("newPassword");
        String oldPassword = request.getParameter("oldPassword");
        String md5password = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        String newmd5password = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        String loginuser = (String) session.getAttribute("loginname");
        User user1 = new User();
        user1.setLoginname(loginuser);
        User realuser = userService.selectUserByAny(user1);
        String msg;
        if (realuser.getPassword().equals(md5password)){
            user.setPassword(newmd5password);
            userService.updateUser(loginuser,user);
            msg = "success";
            message.setJson(msg);
        }else {
            message.setJson("密码错误");
        }

        return message;
    }

    /**
     * 更新用户信息
     * @param request
     * @return
     */
    @PostMapping("/user/update")
    public Message updateUser(HttpServletRequest request){
        String oldloginame = request.getParameter("oldname");
        String loginname = request.getParameter("loginname");
        String username = request.getParameter("username");
        Integer status = null;
        try {
            status = Integer.valueOf(request.getParameter("status"));
        } catch (NumberFormatException e) {
            log.error("没有指定身份");
        }
        user.setLoginname(loginname);
        user.setUsername(username);
        if (status!=null){
            user.setStatus(status);
            if (status==1){
                user.setPerms("user:admin");
            }
        }
        userService.updateUser(oldloginame,user);
        message.setJson("更新成功");
        return message;
    }



    /**
     * 添加新用户
     * @return
     */
    @PostMapping("/user/insert")
    public Message insert(HttpServletRequest request){
        String username = request.getParameter("username");
        String loginname = request.getParameter("loginname");
        int status = Integer.parseInt(request.getParameter("status"));
        String password = request.getParameter("password");
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());

        user.setUsername(username);
        user.setPassword(md5password);
        user.setLoginname(loginname);
        user.setStatus(status);
        if (status==1){
            user.setPerms("user:admin");
        }
        userService.insertUser(user);
        // mailService.sendMail("870839344@qq.com","1608881588@qq.com",
        //         "注册成功消息通知","成功注册用户："+loginname);
        message.setJson("添加成功");
        return message;
    }

    /**
     * 查找所有用户
     * @return
     */
    // @Async
    @GetMapping("/user/findall")
    public List<User> findAll(HttpServletRequest request) {
        Integer status = null;
        try {
            status = Integer.valueOf(request.getParameter("status"));
        } catch (NumberFormatException e) {
            log.error("查询所有用户");
        }
        User user = new User();
        log.error("状态{}",status);
        if (status != null) {
            user.setStatus(status);
        }
        List<User> users = userService.queryAll(user);
        log.error("所有的用户{}",users);
        return users;
    }

    /**
     * 查找指定用户
     * @param request
     * @return
     */
    @GetMapping("/user/findone")
    public User findone(HttpServletRequest request){
        String name = request.getParameter("loginname");
        User user1 = new User();
        Integer status = null;
        try {
            status = Integer.valueOf(request.getParameter("status"));
            user1.setStatus(status);
        } catch (NumberFormatException e) {
            System.out.println(e);
        }
        log.error("用户信息：{}",name);
        user1.setLoginname(name);
        User user = userService.selectUserByAny(user1);
        return user;
    }

    /**
     * 删除多个用户
     * @param request
     * @return
     */
    @GetMapping("/user/removeUser")
    public Message removeUser(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
             split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        userService.deleteUserByids(list);
        message.setJson("删除成功"+ids);
        return message;
    }

    /**
     * 分页查询数据
     */
    @PostMapping("/user/findcurrent")
    public Pages selectPage(long current, long size){
        Pages pages = userService.selectPage(current, size);
        return pages;
    }


}
